<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CommonForm extends FormRequest
{
    protected $tempRules = [    
    ];
    //验证规则0
    protected $rules = [    
    ];
    //验证规则1
    protected $rules1 = [    
        'name' => 'required|max:255', //name 必填
        'active' => 'sometimes|accepted', //yes、on、1或true
        'sort' => 'sometimes|integer', //必须是整形
    ];
    //验证规则2
    protected $rules2 = [    
        'name' => 'required|max:255', //name 必填 唯一
        'content' => 'required', //name 必填
        'active' => 'sometimes|accepted', //yes、on、1或true
        'sort' => 'sometimes|integer', //必须是整形
    ];
    //验证规则3
    protected $rules3 = [    
        'ids' => 'required|max:255', //ids 必填 要删除的id值
    ];

    //验证字段
    protected $strings_key = [
        'name' => '标题',
        'content' => '内容',
        'active' => '激活字段',
        'sort' => '排序字段',
        'ids' => '删除项',
    ];
    //验证提示消息
    protected $strings_val = [
        'required'=> '为必填项',
        'min'=> '最小为:min',
        'max'=> '最大为:max',
        'between'=> '长度在:min和:max之间',
        'integer'=> '必须为整数',
        'accepted'=> '值为yes、on、1或true',
        'sometimes'=> '',
        'unique'=> '要唯一，不能重复',
    ];
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = $this->rules1;
        //获取控制器及方法名
        $action = request()->route()->getActionName();
        list($class, $method) = explode('@', $action);
        $class = substr(strrchr($class,'\\'),1);

        //获取编辑的时候的参数 如果需要设置表单唯一的时候可用
        $schema = strtolower(str_replace('Controller','',$class));
        $id = $this->route($schema);

        //表单name唯一值,需要时直接替换 $rules['name']项 $rules['name'] = $unique;
        $unique='required|unique:'.$schema.'s,name,'.$id.'|max:255';

        if($method == 'delete'){
            $rules = $this->rules3;
        }

        //根据控制器或者方法名重置规则
        switch ($class) {
            //文章控制器
            case 'ArticleController':
                switch ($method) {
                    //存储
                    case 'store':
                        $rules = $this->rules2;
                        break;
                    //存储
                    case 'update':
                        $rules = $this->rules2;
                        break;
                    
                    default:
                        # code...
                        break;
                }
                break;
            case 'RoleController':
                if($method == 'delete'){
                    $rules = $this->rules3;
                } else {
                    $rules['name'] = $unique;
                }
                break;
            case 'OperationController':
                if($method == 'delete'){
                    $rules = $this->rules3;
                } else {
                    $rules['name'] = $unique;
                }
                break;
            default:
                # code...
                break;
        }
        $this->tempRules = $rules;
        return $rules;
    }

    /**
     * 获取被定义验证规则的错误消息
     *
     * @return array
     * @translator laravelacademy.org
     */
    public function messages(){
        $rules = $this->tempRules;
        $k_array = $this->strings_key;
        $v_array = $this->strings_val;
        foreach ($rules as $key => $value) {
            $new_arr = explode('|', $value);//分割成数组
            foreach ($new_arr as $k => $v) {
                $head = strstr($v,':',true);//截取:之前的字符串
                if ($head) {$v = $head;}
                $array[$key.'.'.$v] = $k_array[$key].$v_array[$v];                  
            }
        }
        return $array;
    }
}
